package cn.kdyzm.minio;

import cn.kdyzm.minio.client.DownloadMinioClient;
import cn.kdyzm.minio.client.UploadMinioClient;
import io.minio.MinioClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author kdyzm
 * @date 2022/11/18
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({MinioProperties.class})
@ComponentScan(basePackages = "cn.kdyzm.minio")
public class MinioAutoConfiguration {

    /**
     * 上传的client
     *
     * @param minioProperties 配置文件
     * @return 上传客户端
     */
    @Bean
    public UploadMinioClient uploadMinioClient(MinioProperties minioProperties) {
        MinioClient build = MinioClient.builder()
                .endpoint(minioProperties.getUploadEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        return new UploadMinioClient(build);
    }

    /**
     * 下载的client
     *
     * @param minioProperties 配置文件
     * @return 下载客户端
     */
    @Bean
    public DownloadMinioClient downloadMinioClient(MinioProperties minioProperties) {
        MinioClient build = MinioClient.builder()
                .endpoint(minioProperties.getDownloadEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        return new DownloadMinioClient(build);
    }
}
